<template>
    <!-- 列表页面 -->
    <div class="tableSample" v-if="!showEdit">
        <div class="header">
            <div class="header-left">
                <p class="title">取送员列表</p>
            </div>
            <div class="header-right">
                <lin-search @query="onQueryChange" placeholder="请输入取送员姓名"/>
            </div>
            <el-select  size="medium" filterable v-model="lin_user_id" @change="handleChangeAdmin" placeholder="请选择酒店后台" clearable v-if="isSuper">
                <el-option
                        v-for="(admin, index) in adminArr"
                        :key="index"
                        :label="admin.username"
                        :value="admin.id">
                </el-option>
            </el-select>
        </div>
        <!-- 表格 -->
        <el-table
                :data="tableData"
                stripe
                border
                v-loading="loading"
        >
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="name"
                    label="姓名">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="phone"
                    label="电话">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="admin_user.username"
                    label="门店">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="create_time"
                    label="设置时间">
            </el-table-column>
            <!-- 操作列 -->
            <el-table-column label="操作" fixed="right" width="170">
                <template slot-scope="scope">
                    <el-button
                            type="primary"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleEdit(scope.row)"
                    >编辑</el-button>
                    <el-button
                            type="danger"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :background="true"
                    :page-size="pageCount"
                    :current-page="currentPage"
                    v-if="refreshPagination"
                    layout="total, prev, pager, next, jumper"
                    :total="total_nums"
            ></el-pagination>
        </div>
    </div>
    <!-- 添加页面 -->
    <staff-edit v-else-if="showEdit" @editClose="editClose" :staffId="staffId"></staff-edit>
</template>
<script>
import staff from '@/models/staff'
import admin from '@/models/admin'
import LinSearch from '@/components/base/search/lin-search'
import StaffEdit from './StaffEdit'
import User from '@/lin/models/user'

export default {
  components: {
    StaffEdit,
    LinSearch,
  },
  data() {
    return {
      tableData: [],
      loading: false,
      isSuper: false,
      showEdit: false,
      staffId: 0,
      lin_user_id: '',
      search: {
        name: '',
      },
      searchAdmin: '',
      adminArr: [],
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 10, // 每页10条数据
      total_nums: 0, // 分组内的总数
    }
  },
  computed: {},
  created() {
    this.getAdminUser()
    this.getAdminAll()
  },
  methods: {
    async getAdminAll() {
      this.adminArr = await admin.getAdminAll()
    },
    async getAdminUser() {
      this.loading = true
      const res = await User.getInformation()
      this.isSuper = res.isSuper
      if (res.linUserId > 1) {
        this.lin_user_id = res.linUserId
      }
      // 获取数据
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleDelete(val) {
      this
        .$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(async () => {
          this.loading = true
          const res = await staff.deleteStaff(val.id)
          if (res.error_code === 0) {
            this.$message.success(`${res.msg}`)
          }
          this._getTableData(this.currentPage, this.pageCount)
        })
    },
    // 获取数据
    async _getTableData(start, count) {
      const res = await staff.getStaffList(start, count, { name: this.search.name, lin_user_id: this.lin_user_id })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
    // 改变管理员
    handleChangeAdmin() {
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleEdit(val) {
      this.showEdit = true
      this.staffId = val.id
    },
    rowClick(val) {
      console.log(val)
    },
    editClose() {
      this.showEdit = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 搜索姓名
    onQueryChange(query) {
      this.search.name = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
}
</script>

<style lang="scss" scoped>
    .tableSample {
        padding: 0 0 30px;

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 30px;
            border-bottom: 1px solid #dae1ed;

            .header-left {
                float: left;

                .title {
                    height: 59px;
                    line-height: 59px;
                    color: $parent-title-color;
                    font-size: 16px;
                    font-weight: 500;
                }
            }

            .header-right {
                float: right;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }

        .table-main {
            padding: 0 30px;
            margin-top: 30px;
        }

        .top-operate {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            margin-top: 30px;
        }

        .sort-input {
            width: 50px;
            background: none;
            justify-content: space-around;
        }

        .table-edit {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .content {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .cell-icon {
                cursor: pointer;
                color: #3963bc;
                font-size: 16px;
            }

            .cell-icon-edit {
                display: flex;
                margin-left: 20px;
                width: 50px;
                justify-content: space-between;

                .cell-cancel {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                }

                .cell-save {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                    margin-right: -20px;
                }
            }
        }

        .m-20 {
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .summary {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;

            img {
                width: 135px;
                height: 200px;
            }

            .demo-table-expand {
                font-size: 0;
                margin-left: 30px;
                display: flex;
                flex-direction: column;

                label {
                    width: 90px;
                    color: #99a9bf;
                }

                .el-form-item {
                    margin-right: 0;
                    margin-bottom: 0;
                    display: flex;
                    flex-direction: row;
                }
            }
        }

        .pagination {
            display: flex;
            justify-content: flex-end;
            margin: 20px;
        }
    }
    // dialog
    .tableSample /deep/ .el-dialog__footer {
        text-align: left;
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__header {
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__body {
        padding: 30px;
    }
</style>
